// TerrainMoveToolPanel.cpp : implementation file
//

#include "StdAfx.h"
#include "TerrainMoveToolPanel.h"
#include "TerrainMoveTool.h"

#include "CryEditDoc.h"

#include <I3DEngine.h>
#include "TerrainMoveTool.h"

#include "Terrain/Heightmap.h"

/////////////////////////////////////////////////////////////////////////////
// CTerrainMoveToolPanel dialog


CTerrainMoveToolPanel::CTerrainMoveToolPanel(CTerrainMoveTool *tool,CWnd* pParent /*=NULL*/)
	: CXTResizeDialog(CTerrainMoveToolPanel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTerrainMoveToolPanel)
	//}}AFX_DATA_INIT

	m_tool = tool;
	Create( IDD,pParent );

	assert( tool != 0 );
}

void CTerrainMoveToolPanel::DoDataExchange(CDataExchange* pDX)
{
	CXTResizeDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTerrainMoveToolPanel)
	DDX_Control(pDX, IDC_ROT_TARGET, m_cbTargetRot);
	DDX_Control(pDX, IDC_SELECT_SOURCE, m_selectSource);
	DDX_Control(pDX, IDC_SELECT_TARGET, m_selectTarget);
	DDX_Control(pDX, IDC_COPY, m_Copy);
	DDX_Control(pDX, IDC_MOVE, m_Move);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTerrainMoveToolPanel, CXTResizeDialog)
	//{{AFX_MSG_MAP(CTerrainMoveToolPanel)
	ON_CBN_SELCHANGE(IDC_ROT_TARGET, OnChangeTargetRot)

	ON_EN_UPDATE(IDC_DYMX, OnUpdateNumbers)
	ON_EN_UPDATE(IDC_DYMY, OnUpdateNumbers)
	ON_EN_UPDATE(IDC_DYMZ, OnUpdateNumbers)
	ON_BN_CLICKED(IDC_SELECT_SOURCE, OnSelectSource)
	ON_BN_CLICKED(IDC_SELECT_TARGET, OnSelectTarget)
	ON_BN_CLICKED(IDC_COPY, OnCopyButton)
	ON_BN_CLICKED(IDC_MOVE, OnMoveButton)
	ON_BN_CLICKED(IDC_SYNC_HEIGHT, OnSyncHeight)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTerrainMoveToolPanel message handlers

BOOL CTerrainMoveToolPanel::OnInitDialog() 
{
	CXTResizeDialog::OnInitDialog();

	CHeightmap *pHeightmap = GetIEditor()->GetHeightmap();
	Vec3 max = pHeightmap->HmapToWorld(CPoint(pHeightmap->GetWidth(), pHeightmap->GetHeight()));

	((CButton*)GetDlgItem(IDC_SYNC_HEIGHT))->SetCheck( m_tool->GetSyncHeight() ? 1 : 0 );

	m_cbTargetRot.AddString("0 degrees");
	m_cbTargetRot.AddString("90 degrees");
	m_cbTargetRot.AddString("180 degrees");
	m_cbTargetRot.AddString("270 degrees");

	m_cbTargetRot.SetCurSel(m_tool->GetTargetRot());

	m_dymX.Create( this,IDC_DYMX );
	m_dymX.SetRange( 0, max.x);
	m_dymX.SetValue(m_tool->GetDym().x);

	m_dymY.Create( this,IDC_DYMY );
	m_dymY.SetRange( 0, max.y);
	m_dymY.SetValue(m_tool->GetDym().y);

	m_dymZ.Create( this,IDC_DYMZ );
	m_dymZ.SetRange( 0, 10000);
	m_dymZ.SetValue(m_tool->GetDym().z);

	UpdateButtons();

	SetResize(IDC_SELECT_SOURCE, SZ_HORRESIZE(0.5f));
	SetResize(IDC_SELECT_TARGET, CXTResizeRect(0.5f, 0.0f, 1.0f, 0.0f));
	SetResize(IDC_COPY, SZ_HORRESIZE(0.5f));
	SetResize(IDC_MOVE, CXTResizeRect(0.5f, 0.0f, 1.0f, 0.0f));

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnCopyButton() 
{
	bool bOnlyVegetation = false;
	bool bOnlyTerrain = false;
	CButton * but = (CButton *) GetDlgItem(IDC_ONLY_VEGETATION);
	if(but)
		bOnlyVegetation = but->GetCheck();
	but = (CButton *) GetDlgItem(IDC_ONLY_TERRAIN);
	if(but)
		bOnlyTerrain = but->GetCheck();
	m_tool->Move(true, bOnlyVegetation, bOnlyTerrain);
}

//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnMoveButton() 
{
	bool bOnlyVegetation = false;
	bool bOnlyTerrain = false;
	CButton * but = (CButton *) GetDlgItem(IDC_ONLY_VEGETATION);
	if(but)
		bOnlyVegetation = but->GetCheck();
	but = (CButton *) GetDlgItem(IDC_ONLY_TERRAIN);
	if(but)
		bOnlyTerrain = but->GetCheck();
	m_tool->Move(false, bOnlyVegetation, bOnlyTerrain);
}

//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnSelectSource() 
{
	m_selectSource.SetCheck(1);
	m_selectTarget.SetCheck(0);
	m_tool->Select(1);
}

//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnSelectTarget() 
{
	m_selectSource.SetCheck(0);
	m_selectTarget.SetCheck(1);
	m_tool->Select(2);
}

//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnUpdateNumbers() 
{
	m_tool->SetDym(Vec3(m_dymX.GetValue(), m_dymY.GetValue(), m_dymZ.GetValue()));
}

//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::UpdateButtons()
{
	CWnd * but = 0;
	if(m_tool->m_source.isCreated && m_tool->m_target.isCreated)
	{
		if(but = GetDlgItem(IDC_COPY))
			but->EnableWindow(true);
		if(but = GetDlgItem(IDC_MOVE))
			but->EnableWindow(true);
	}
	else
	{
		if(but = GetDlgItem(IDC_COPY))
			but->EnableWindow(false);
		if(but = GetDlgItem(IDC_MOVE))
			but->EnableWindow(false);
	}
}


//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnChangeTargetRot()
{
	int rot=m_cbTargetRot.GetCurSel();
	if( rot<0 )
		rot = 0;
	m_tool->SetTargetRot(rot);
}


//////////////////////////////////////////////////////////////////////////
void CTerrainMoveToolPanel::OnSyncHeight()
{
	m_tool->SetSyncHeight(((CButton*)GetDlgItem(IDC_SYNC_HEIGHT))->GetCheck() == 1);
}